package net.csdra.cms.wordpress.config;

import java.io.IOException;
import javax.sql.DataSource;
import net.csdra.cms.api.service.CmsService;
import net.csdra.cms.wordpress.repository.WordPressAdapter;
import net.csdra.cms.wordpress.service.WordpressCmsServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * The Class AppConfig.
 *
 * @author Didy
 */
@Configuration
@EnableTransactionManagement
@EnableAspectJAutoProxy
@ImportResource("classpath:provider.xml")
public class AppConfig {

    @Value("#{systemProperties['db.url']?:'jdbc:mysql://localhost:3306/wordpress?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8'}")
    String dbUrl;

    @Value("#{systemProperties['db.user']?:'root'}")
    String dbUser;

    @Value("#{systemProperties['db.password']?:''}")
    String dbPassword;

    @Bean
    WordPressAdapter wordPressAdapter() {
        return new WordPressAdapter();
    }

    @Bean
    public CmsService cmsService() {
        return new WordpressCmsServiceImpl();
    }

    /**
     * Entity manager factory bean.
     *
     * @return the local container entity manager factory bean
     */
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() throws IOException {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        return em;
    }

    /**
     * Data source.
     *
     * @return the data source
     * @throws java.io.IOException
     */
    @Bean
    public DataSource dataSource() throws IOException {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        System.out.println("dbUrl:" + dbUrl);
        System.out.println("dbUser:" + dbUser);
        System.out.println("dbPassword:" + dbPassword);
        dataSource.setUrl(dbUrl);
        dataSource.setUsername(dbUser);
        dataSource.setPassword(dbPassword);

        return dataSource;
    }

    /**
     * Transaction manager.
     *
     * @return the platform transaction manager
     */
    @Bean
    public PlatformTransactionManager transactionManager() throws IOException {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
        return transactionManager;
    }

}
